package com.huang.mapper;

import com.huang.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper//相当于Dao层 操作数据库
public interface UserMapper {

    @Select("select * from t_users where id = #{id}")
    User selectUserById(Integer id);

    @Select(" SELECT id,name,password,sex,birthday,registTime\n" +
            "        FROM t_users\n" +
            "        WHERE id = #{id}\n" +
            "          AND password = #{pwd}")
    User selectUserByIdAndPwd(@Param("id")Integer id ,  @Param("pwd") String pwd);

    @Select(" SELECT id,name,password,sex,birthday,registTime\n" +
            "        FROM t_users\n" +
            "        WHERE id = #{id}\n" +
            "          AND password = #{pwd}")
    User selectUserByIdAndPwd4(Map values);


    @Select(" SELECT id,name,password,sex,birthday,registTime\n" +
            "        FROM t_users\n" +
            "        WHERE id = #{id}\n" +
            "          AND password = #{password}")
    User selectUserByUserInfo(User user);

    List<User> selectUsersByKeyword(String keyword);

    @Delete("DELETE FROM t_users WHERE id = #{id}")
    void deleteUser(Integer id);

    @Update(" UPDATE t_users SET name=#{name}, password=#{password}, " +
            "sex=#{sex}, birthday=#{birthday}\n" +
            "    WHERE id = #{id}")
    void updateUser(User user);


    //主键回填
    @Options(useGeneratedKeys = true, keyProperty = "id",keyColumn = "id")
    @Insert("INSERT INTO t_users " +
            "       VALUES (#{id}, #{name}, " +
            "#{password}, #{sex}, #{birthday}, #{registTime});")
    void insertUser(User user);

    @Select("select * from t_users where id = #{id}")
    Map<String,Object> selectMapByUserId(Integer id);
}